﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using AnguaRPGEngine.Library.Systems;

namespace AnguaRPGEngine.Library.Dice
{
    public class D10 : IDie
    {
        #region ctor
        public D10()
        {
        }
        #endregion
        #region IDie Members
        #region Image
        public System.Drawing.Bitmap Image
        {
            get
            {
                Font frontFirstDigitFont = new Font(FontFamily.GenericSansSerif, 20f);
                Font frontSecondDigitFont = new Font(FontFamily.GenericSansSerif, 16f);
                Font sideFont = new Font(FontFamily.GenericSansSerif, 12f);
                Bitmap i = ImageLibrary.Instance.GetImageFromLibrary(string.Format("Dice.{0}.D{1}", DieRoller.Instance.DiceCollection.CurrentSet.PathName, Sides, _Value));
                using (Graphics g = Graphics.FromImage(i))
                {
                    string onesSpot = string.Format("{0}", Value);
                    if (Value == 10)
                    {
                        onesSpot = "0";
                    }

                    if (Value == 6 || Value == 9)
                    {
                        g.DrawLine(new Pen(new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), 1f), new Point(30, 52), new Point(40, 52));
                    }
                    if (SelectSideToTheLeft(onesSpot) == "6" || SelectSideToTheLeft(onesSpot) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(3, 30), new Point(8, 38));
                    }
                    if (SelectSideToTheRight(onesSpot) == "6" || SelectSideToTheRight(onesSpot) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(66, 40), new Point(69, 30));
                    }
                   
                    
                    g.DrawString(onesSpot, frontFirstDigitFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(23, 23));

                    Matrix matrix = new Matrix();
                    matrix.Rotate(55);
                    g.Transform = matrix;
                    g.DrawString(SelectSideToTheLeft(onesSpot), frontFirstDigitFont, new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(20, -12));
                    g.ResetTransform();
                    matrix.Rotate(-110);
                    g.Transform = matrix;
                    g.DrawString(SelectSideToTheRight(onesSpot), frontFirstDigitFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-3, 50));
                }
                return i;
            }
        }
        #endregion
        #region Sides
        public int Sides
        {
            get
            {
                return 10;
            }
        }
        #endregion
        #region Value
        private int _Value = 1;
        public int Value
        {
            get
            {
                return _Value;
            }
        }
        #endregion
        #region Roll
        public void Roll()
        {
            _Value = Randomizer.Instance.NextInt(1, Sides);
        }
        #endregion
        #endregion
        private string SelectSideToTheLeft(string onesSpot)
        {
            int ts = int.Parse(onesSpot);
            switch (ts)
            {
                case 1:
                    return "9";
                case 2:
                    return "6";
                case 3:
                    return "7";
                case 4:
                    return "0";
                case 5:
                    return "3";
                case 6:
                    return "4";
                case 7:
                    return "1";
                case 8:
                    return "2";
                case 9:
                    return "5";
            }
            return "8";
        }
        private string SelectSideToTheRight(string onesSpot)
        {
            int ts = int.Parse(onesSpot);
            switch (ts)
            {
                case 1:
                    return "7";
                case 2:
                    return "8";
                case 3:
                    return "5";
                case 4:
                    return "6";
                case 5:
                    return "9";
                case 6:
                    return "2";
                case 7:
                    return "3";
                case 8:
                    return "0";
                case 9:
                    return "1";
            }
            return "4";
        }
    }
}
